package com.easidea.tim.flex.remote.system
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.bean.SysLocator;
	import com.easidea.tim.flex.bean.system.Staff;
	import com.easidea.tim.flex.bean.system.permission.Permission;
	import com.easidea.tim.flex.bean.system.permission.Role;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	import com.easidea.tim.flex.util.*;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import net.digitalprimates.persistence.hibernate.rpc.HibernateRemoteObject;
	public class EmployeeDao extends BaseDao
	{
	   private var _employeeService:HibernateRemoteObject = new HibernateRemoteObject();
	   [Bindable] private var sys:SysLocator = SysLocator.getInstance();
	   private static var _instance:EmployeeDao;
	   private var _dispatchType:String;
		/**
		 * 返回Dao实例
		 * @return 
		 * 
		 */		
		public static function getInstance():EmployeeDao{
			if( !_instance ){
				_instance = new EmployeeDao();
				
			}
			return _instance;
		}
		/**
		 * 
		 * 构造函数
		 */		
		public function EmployeeDao() {
				_employeeService.channelSet = this.buildChannelSet();
				_employeeService.destination="staffService";	
				_employeeService.concurrency = "last";
				_employeeService.makeObjectsBindable = true;	
				_employeeService.showBusyCursor = true;
		}
		/**
		 * 调用Spring方法：根据条件查询员工列表
		 * @param employeeName
		 * @param dutyDate
		 * @param deptId
		 * @param email
		 * @param page
		 * 
		 */		
		public function listStaff(employeeName:String,dutyDate:Date,deptId:int,email:String,page:Page):void {
			_employeeService.addEventListener(ResultEvent.RESULT, listStaffSuccess);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.listStaff(employeeName,dutyDate,deptId,email,page);
			
		}
		/**
		 * 根据条件查询员工列表成功回调函数
		 * @param e
		 * 
		 */		
		private function listStaffSuccess(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, listStaffSuccess);
			 if(e.result != null) {	 
			 		 Env.dispatchEvent(new CustomerEvent("listStaffSuccess",e.result));	  										 
			 } 
		}		
		/**
		 * 调用spring方法：新增或修改员工保存
		 * @param employee
		 * 
		 */		
		public function saveEmployee(employee:Staff):void {
			_employeeService.addEventListener(ResultEvent.RESULT, saveEmployeeSuccess);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.createOrUpdateStaff(employee);
			
		}
		/**
		 * 新增或修改员工保存成功回调函数
		 * @param e
		 * 
		 */		
		private function saveEmployeeSuccess(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, saveEmployeeSuccess);
			 if(e.result != null && e.result == "success") {
				AppAlert.show("保存成功！","提示");
				Env.dispatchEvent(new CustomerEvent("saveEmployeeSuccess"));
			 } else if(e.result == "ExistSameName"){
			 	//AppAlert.show("存在相同的客户类别名称，请重新输入！","提示");
			 	Env.dispatchEvent(new CustomerEvent("saveEmployeeFail",e.result));
			 }else {
			 	//AppAlert.show(e.result as String,"提示");
			 	trace(e.message);
			 	Env.dispatchEvent(new CustomerEvent("saveEmployeeFail",e.result));
			 }				 
		}
		/**
		 * 调用spring方法：删除员工
		 * @param id
		 * 
		 */		
		public function deleteEmployee(id:int):void {
			_employeeService.addEventListener(ResultEvent.RESULT, deleteEmployeeSuccess);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.deleteEmployee(id);
			
		}
		/**
		 * 删除员工成功回调函数
		 * @param e
		 * 
		 */		
		private function deleteEmployeeSuccess(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, deleteEmployeeSuccess);
			 if(e.result != null) {		
				AppAlert.show("删除成功！","提示信息");	
				Env.dispatchEvent(new CustomerEvent("deleteEmployeeSuccess"));	  									 
			 } 
		}	
		/**
		 * 调用spring方法：删除员工
		 * @param id
		 * 
		 */		
		public function getAllRole():void {
			_employeeService.addEventListener(ResultEvent.RESULT, getAllRoleSuccess);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.getAllRole();
			
		}
		/**
		 * 删除员工成功回调函数
		 * @param e
		 * 
		 */		
		private function getAllRoleSuccess(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, getAllRoleSuccess);
			 if(e.result != null) {		
				Env.dispatchEvent(new CustomerEvent("getAllRoleSuccess",e.result));	  									 
			 } 
		}	
		/**
		 * 调用spring方法：新增或修改员工保存
		 * @param employee
		 * 
		 */		
		public function setUpEmployeeRole(employee:Staff):void {
			_employeeService.addEventListener(ResultEvent.RESULT, setUpEmployeeRoleSuccess);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.createOrUpdateStaff(employee);
			
		}
		/**
		 * 新增或修改员工保存成功回调函数
		 * @param e
		 * 
		 */		
		private function setUpEmployeeRoleSuccess(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, setUpEmployeeRoleSuccess);
			 if(e.result != null) {		
				AppAlert.show("权限设置成功！","提示信息");	
				Env.dispatchEvent(new CustomerEvent("setUpEmployeeRoleSuccess"));	  									 
			 } 
		}
		/**
		 * 获取员工的授信额度 
		 * @param staffId
		 * @param dispatchType
		 * 
		 */		
		public function getStaffCreditLimitByStaffId(staffId:int,dispatchType:String):void {
			_dispatchType = dispatchType;
		  	_employeeService.addEventListener(ResultEvent.RESULT, getStaffCreditLimitByStaffIdResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.getStaffCreditLimitByStaffId(staffId);	
		}
		/**
		 * 获取员工的授信额度结果回调 
		 * @param e
		 * 
		 */		
		private function getStaffCreditLimitByStaffIdResult(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, getStaffCreditLimitByStaffIdResult);
			Env.dispatchEvent(new CustomerEvent(_dispatchType,e.result));	  									 
			  
		}
		/**
		 * 初始化员工的授信额度 
		 * @param staffId
		 * @param dispatchType
		 * 
		 */		
		public function initStaffCreditLimit(staffId:int,initCreditLimit:Number,
							creator:String,createId:int):void {
		  	_employeeService.addEventListener(ResultEvent.RESULT, initStaffCreditLimitResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.initStaffCreditLimit(staffId,initCreditLimit,creator,createId);	
		}
		private function initStaffCreditLimitResult(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, initStaffCreditLimitResult);
			if(e.result != null && String(e.result) == "success") {
				Env.dispatchEvent(new CustomerEvent("initStaffCreditLimitSuccess"));
			}
		}	
		/**
		 * 初始化员工的授信额度 
		 * @param staffId
		 * @param dispatchType
		 * 
		 */		
		public function modifyStaffCreditLimit(staffId:int,currentCreditLimit:Number,modifyCreditLimit:Number,
							modifier:String,modifierId:int):void {
		  	_employeeService.addEventListener(ResultEvent.RESULT, modifyStaffCreditLimitResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.modifyStaffCreditLimit(staffId,currentCreditLimit,modifyCreditLimit,modifier,modifierId);	
		}
		private function modifyStaffCreditLimitResult(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, modifyStaffCreditLimitResult);
			if(e.result != null && String(e.result) == "success") {
				Env.dispatchEvent(new CustomerEvent("modifyStaffCreditLimitSuccess"));
			}
		}	
		/**
		 * 登陆
		 * @param userName
		 * @param password
		 * 
		 */		
		public function doLogin(userName:String,password:String):void {
			_employeeService.addEventListener(ResultEvent.RESULT, doLoginResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.doLogin(userName,password);			
		}
		/**
		 * 登陆结果回调 
		 * @param e
		 * 
		 */		
		private function doLoginResult(e:ResultEvent):void {
			_employeeService.removeEventListener(ResultEvent.RESULT, doLoginResult);
			 if(e.result != null) {	
			 	var staff:Staff = e.result as Staff;
			 	sys.loggedUserId = staff.id;
			 	sys.loggedName = staff.name;
			 	sys.loggedUser = 	staff.username;
			 	var roleList:ArrayCollection = staff.roleList;
			 	for each(var role:Role in roleList) {
			 		var permissions:ArrayCollection = role.permissions;
			 		for each(var permission:Permission in permissions){
			 			sys.staffPermissions.addItem(permission);
			 		}
			 	} 
				Env.dispatchEvent(new CustomerEvent("loginSuccess",staff));
					  									 
			 } else {
			 	Env.dispatchEvent(new CustomerEvent("loginFail"));
			 }
		}
		/**
		 *注销
		 *
		 */		
		public function doLogout():void {
			sys.loggedUser = null;
			sys.staffPermissions.removeAll();
			Env.dispatchEvent(new CustomerEvent("logoutSuccess"));
		}
		
		
		/**
		 *  过滤员工信息
		 *  @param string 员工姓名
		 *  @author Harrison
		 */
		public function requestFilteStuff(name:String):void{
			_employeeService.addEventListener(ResultEvent.RESULT, doFilteStuff);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.filteStuff(name);		
		}
		private function doFilteStuff(e:ResultEvent):void{
			_employeeService.removeEventListener(ResultEvent.RESULT, doFilteStuff);
			if(e.result != null){
				var stuffList:ArrayCollection = e.result as ArrayCollection;
				Env.dispatchEvent(new CustomerEvent("filteStuffSuccess",e.result));	 
			}
		}
		/**
		 *  获取组织员工信息 
		 *  @param 登录员工id
		 *  @author Harrison
		 */
		public function getTeamworks(id:int):void{
			_employeeService.addEventListener(ResultEvent.RESULT, getTeamworksResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.getTeamworks(id);		
		}
		/**
		 * 获取组织员工信息回调
		 * @param e
		 * 
		 */		
		private function getTeamworksResult(e:ResultEvent):void{
			_employeeService.removeEventListener(ResultEvent.RESULT, getTeamworksResult);
			if(e.result != null){			
				Env.dispatchEvent(new CustomerEvent("getTeamworksResult",e.result));	 
			}
		}		
/**
		 *  获取组织员工信息
		 *  @param 登录员工id
		 *  @author Harrison
		 */
		public function saveStaffTeamwork(parentId:int, children:Array,recursionSearch:int):void{
			_employeeService.addEventListener(ResultEvent.RESULT, saveStaffTeamworkResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.saveStaffTeamwork(parentId,children,recursionSearch);		
		}
		/**
		 * 获取组织员工信息回调
		 * @param e
		 * 
		 */		
		private function saveStaffTeamworkResult(e:ResultEvent):void{
			_employeeService.removeEventListener(ResultEvent.RESULT, saveStaffTeamworkResult);
			if(e.result != null){			
				Env.dispatchEvent(new CustomerEvent("saveStaffTeamworkResult",e.result));	 
			}
		}		
		
		/**
		 * 	获取销售员信用额度，已经欠款总额
		 * 
		 * */
		public function getStaffCurrentCreditInfo(creator:String):void{
			_employeeService.addEventListener(ResultEvent.RESULT, getStaffCurrentCreditInfoResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.getStaffCurrentCreditInfo(creator);		
		}		
		
		/**
		 * 获取销售员信用额度回调
		 * 
		 * */
		private function getStaffCurrentCreditInfoResult(e:ResultEvent):void{
			_employeeService.removeEventListener(ResultEvent.RESULT,getStaffCurrentCreditInfoResult);
			Env.dispatchEvent(new CustomerEvent("getStaffCurrentCreditInfoSuccess",e.result));
		}
		
		/**
		 * 提取员工信息
		 */
		public function getStaffInfo(id:int):void{
			_employeeService.addEventListener(ResultEvent.RESULT, getStaffInfoResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.getStaffById(id);
		}
		
		private function getStaffInfoResult(e:ResultEvent):void{
			_employeeService.removeEventListener(ResultEvent.RESULT,getStaffInfoResult);
			Env.dispatchEvent(new CustomerEvent("getStaffInfoSuccess",e.result));
		}
		
		public function updateEmpPass(id:int, pass:String):void{
			_employeeService.addEventListener(ResultEvent.RESULT, updateEmpPassResult);
			_employeeService.addEventListener(FaultEvent.FAULT, _Fault);
			_employeeService.updateEmpPass(id,pass);
		}
		
		private function updateEmpPassResult(e:ResultEvent):void{
			_employeeService.removeEventListener(ResultEvent.RESULT, updateEmpPassResult);
			Env.dispatchEvent(new CustomerEvent("updateEmpPassSuccess",e.result));
		}
		
	}
}